<template>
  <div>
    <!-- 表单校验三个要素, el-form 上的 model / rules
    el-form-item prop -->
    <!-- 表单容器 -->
    <el-form :model="loginForm" :rules="rules">
      <el-form-item label="手机号" prop="mobile">
        <!-- 每个form-item都是一个表单项, 里面有输入框 -->
        <el-input v-model="loginForm.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="loginForm.password" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input v-model="loginForm.checkPass" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button>登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const checkPassword = (rule, val, callback) => {
      // 这个函数, 是被element ui 调用的自定义校验函数
      // 默认可以得到三个形参 rule规则 val输入值 callback回调
      // 1. 自定义判断逻辑得出当前输入值是否合法的布尔变量
      const isPassword = val === this.loginForm.password
      if (isPassword) {
        callback()
      } else {
        callback(new Error('必须与密码一致！'))
      }
    }
    return {
      loginForm: {
        mobile: '',
        password: '',
        checkPass: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请输入正确的手机号', trigger: 'blur' },
          { pattern: /^1[3-9][0-9]{9}$/, message: '请输入正确的11位手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入6-12位的密码', trigger: 'blur' }

        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: checkPassword, trigger: 'blur' }
        ]
      }
    }
  }
}
</script>

<style>
.box{
    overflow: hidden;
    display: none;
    visibility: hidden;
}
</style>
